import ajax from '../libs/ajax.js';
import tools from '../libs/tools.js';

//#region 首页
let sy=document.getElementById('sy');
sy.addEventListener('click',()=>{
  location.href='./home.html'
})
//#endregion

//#region 留言板信息获取
let txtqtitle = document.getElementById('txtqtitle');
let btnquery = document.getElementById('btnquery');
let list = {
  info: '',
  orderBy: 1,
  username: ''
};
let page = {
  pageNumber: 1,
  pageSize: 10
};
let nr = [];

function hutao() {
  list.info = txtqtitle.value;
  let cs = tools.concatJson(list, page);
  ajax.send('/message/queryAll', cs, (data) => {
    if (data.success) {
      nr = data.list;
      page = data.page;
      zs();
      fy();
    }
  });
}
function zs() {
  let datalist = document.getElementById('datalist');
  datalist.innerHTML = '';
  for (let i = 0; i < nr.length; i++) {
    const x = nr[i];
    //item div0 框
    let div0 = document.createElement('div');
    div0.classList.add('item');
    //img1 图片
    let img1 = document.createElement('img');
    img1.classList.add('avator');
    if (x.userInfo.img) {
      img1.setAttribute('src', x.userInfo.img);
    } else {
      img1.setAttribute('src', 'img/tp10.jpg');
    }
    div0.append(img1);
    datalist.append(div0);

    //div2 内容==========
    let div2 = document.createElement('div');
    div2.classList.add('info');
    //div3 账号
    let div3 = document.createElement('div');
    div3.classList.add('email');
    div3.append(x.user.nickname);
    div2.append(div3);
    div0.append(div2);
    datalist.append(div0);

    //div4 时间
    let div4 = document.createElement('div');
    div4.classList.add('time');
    div4.append(tools.FormData(x.lastupdate));
    div2.append(div4);
    div0.append(div2);
    datalist.append(div0);

    //评论信息
    let div5 = document.createElement('div');
    div5.classList.add('text', 'word3');
    div5.append(x.title);
    div2.append(div5);
    div0.append(div2);
    datalist.append(div0);

    //点赞，举报包裹div6
    let div6 = document.createElement('div');
    div6.classList.add('do');
    div2.append(div6);
    div0.append(div2);
    datalist.append(div0);

    //点赞数量div7
    let div7 = document.createElement('div');
    div7.append(x.praiseCount + '点赞');
    div7.classList.add('good', 'btn');
    div6.append(div7);
    div2.append(div6);
    div0.append(div2);
    datalist.append(div0);

    //点击量
    let div8 = document.createElement('div');
    div8.append(x.hits + '点击量');
    div8.classList.add('look', 'btn');
    div6.append(div8);
    div2.append(div6);
    div0.append(div2);
    datalist.append(div0);

    //评论数量
    let div9 = document.createElement('div');
    div9.append(x.replyCount + '评论');
    div9.classList.add('common', 'btn');
    div6.append(div9);
    div2.append(div6);
    div0.append(div2);
    datalist.append(div0);
  }
}
//#endregion

//#region =========分页============
let fenye1 = document.querySelectorAll('nav>a');
let fenye2 = document.querySelector('nav>span');
function fy() {
  fenye2.innerHTML = `
  记录数/当前页/总页数
  ${page.total}/${page.pageNumber}/${page.pageCount}
  `;
}

fenye1[0].addEventListener('click', () => {
  if (page.pageNumber == 1) {
    return;
  }
  page.pageNumber = 1;
  hutao();
});
fenye1[1].addEventListener('click', () => {
  page.pageNumber--;
  if (page.pageNumber < 1) {
    page.pageNumber = 1;
    return;
  }
  hutao();
});
fenye1[2].addEventListener('click', () => {
  page.pageNumber++;
  if (page.pageNumber > page.pageCount) {
    page.pageNumber = page.pageCount;
    return;
  }
  hutao();
});
fenye1[3].addEventListener('click', () => {
  if (page.pageNumber == page.pageCount) {
    return;
  }
  page.pageNumber = page.pageCount;
  hutao();
});

//#endregion

//#region 搜索
btnquery.addEventListener('click', () => {
  page.pageNumber = 1;
  hutao();
});
//#endregion

hutao();
